package com.tanhua.manage.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.TestOptionApi;
import com.tanhua.dubbo.api.TestPaperApi;
import com.tanhua.dubbo.api.TestQuestionApi;
import com.tanhua.model.mongo.TestOption;
import com.tanhua.model.mongo.TestPaper;
import com.tanhua.model.mongo.TestQuestion;
import com.tanhua.model.vo.OptionVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.PaperVo;
import com.tanhua.model.vo.QuestionVo;
import org.apache.dubbo.config.annotation.DubboReference;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SoulService {
    @DubboReference
    private TestOptionApi optionApi;
    @DubboReference
    private TestQuestionApi questionApi;
    @DubboReference
    private TestPaperApi paperApi;
    //分页查询option
    public PageResult findOptionByPage(Integer page, Integer pageSize) {
        //查询所有选项
        PageResult result=optionApi.findByPage(page,pageSize);
        List<TestOption> list = (List<TestOption>) result.getItems();
        if (CollUtil.isEmpty(list)){
            return result;
        }
        List<String> questionId = CollUtil.getFieldValues(list, "questionId", String.class);
        //查询问题
        Map<String, TestQuestion> map=questionApi.findByIds(questionId);
        //构造返回值
        List<OptionVo> vos=new ArrayList<>();
        for (TestOption option : list) {
            String id = option.getQuestionId();
            TestQuestion question = map.get(id);
            OptionVo init = OptionVo.init(option, question);

            vos.add(init);
        }
        result.setItems(vos);
        return result;
    }

    public Boolean deletOption(String id) {
        Boolean result=optionApi.deleteById(id);
        return result;
    }

    //根据id查询选项
    public TestOption findOptionById(String id) {
        TestOption option= optionApi.findById(id);
        return option;
    }

    //保存option
    public void saveOption(TestOption options) {
        optionApi.save(options);
    }
    //更新option
    public void updateOption(TestOption options,String optionId) {
        System.out.println(optionId);
        optionApi.update(options,optionId);
    }

    //查询所有问题
    public List<QuestionVo> findAllQuestions() {
        List<TestQuestion> list=questionApi.findAll();
        List<QuestionVo> vos=new ArrayList<>();
        for (TestQuestion question : list) {
            QuestionVo init = QuestionVo.init(question);
            vos.add(init);
        }
        return vos;
    }

    //分页查询问题
    public PageResult findQuestionsByPage(Integer page, Integer pageSize) {
        PageResult result=questionApi.findByPage(page,pageSize);
        List<TestQuestion> list = (List<TestQuestion>) result.getItems();
        if (CollUtil.isEmpty(list)){
            return result;
        }
        List<String> paperId = CollUtil.getFieldValues(list, "paperId", String.class);
        //查询试卷
        Map<String, TestPaper> map=paperApi.findByIds(paperId);
        //构造返回值
        List<QuestionVo> vos=new ArrayList<>();
        for (TestQuestion question : list) {
            String id = question.getPaperId();
            TestPaper paper = map.get(id);
            QuestionVo init = new QuestionVo();
            if(paper == null){
                init = QuestionVo.init(question);
            } else {
                init = QuestionVo.init(question,paper);
            }
            vos.add(init);
        }
        result.setItems(vos);
        return result;

    }

    //根据id删除问题
    public void deleteQuestion(String id) {
        //删除问题
        questionApi.deleteById(id);
        //删除问题所带的答案
        optionApi.deleteByQuestionId(id);

    }

    //根据id查询问题
    public TestQuestion findQuestionById(String id) {
        TestQuestion question=questionApi.findById(id);
        return question;
    }
    //保存问题
    public void saveQuestions(TestQuestion questions) {
        questionApi.save(questions);
    }
    //更新问题
    public void updateQuestions(TestQuestion questions,String questionId) {
        System.out.println(questionId);
        questionApi.update(questions,questionId);
    }

    //查询所有试卷
    public List<PaperVo> findAllPapers() {
        List<TestPaper> paperList = paperApi.findAll();
        List<PaperVo> list=new ArrayList<>();
        for (TestPaper paper : paperList) {
            PaperVo init = PaperVo.init(paper);
            list.add(init);
        }
        return list;
    }

    //根据试卷id查询试卷内容
    public Map<String, List<String>> findPaperContentByPaperId(String id) {
        //建立map集合{问题,选项列表}
        Map<String, List<String>> map=new HashMap<>();

        //根据试卷id查询试卷题目
        List<TestQuestion> questionList=questionApi.findByPaperId(id);
        if (CollUtil.isEmpty(questionList)){
            return map;
        }
        //获取所有的问题id
        List<String> queIds = CollUtil.getFieldValues(questionList, "id", String.class);
        //根据id获取选项
        Map<String, List<String>> optionMap=optionApi.findByQuestionIds(queIds);
        //根据问题id获取所有的问题
        Map<String, TestQuestion> queMap = questionApi.findByIds(queIds);
        for (String queId : queIds) {
            TestQuestion question = queMap.get(queId);
            List<String> list = optionMap.get(queId);

            map.put(question.getQuestion(),list);
        }
        return map;
    }

    //分页查询试卷
    public PageResult findPaperByPage(Integer page, Integer pageSize) {

        PageResult result=paperApi.findByPage(page,pageSize);

        //构造返回值
        return result;
    }

    //删除试卷
    public void deletePaper(String id) {
        //删除试卷
        paperApi.delete(id);
        //将问题中的试卷id删除
        questionApi.updateByPaperId(id);
    }
    //跟据id查询试卷
    public TestPaper findPaperById(String id) {
        TestPaper testPaper=paperApi.findById(id);
        return testPaper;
    }
    //保存试卷
    public void savePaper(TestPaper papers) {

        paperApi.save(papers);
    }
    //更新试卷
    public void updatePaper(TestPaper papers,String paperId) {
        System.out.println(paperId);
        paperApi.update(papers,paperId);
    }
}
